#!/bin/bash

# Docker镜像构建脚本
# 专门用于构建 torrentfileschecker 项目

# 设置颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 镜像名称
IMAGE_NAME="torrentfileschecker"
DEFAULT_TAG="1.0.0"

echo -e "${GREEN}======================================${NC}"
echo -e "${GREEN}  Docker镜像构建脚本${NC}"
echo -e "${GREEN}======================================${NC}"
echo ""

# 提示用户输入tag
echo -e "${YELLOW}请输入镜像标签 (默认: ${DEFAULT_TAG}):${NC}"
read -r TAG_INPUT

# 如果用户没有输入，使用默认值
if [ -z "$TAG_INPUT" ]; then
    TAG="$DEFAULT_TAG"
    echo -e "${GREEN}使用默认标签: ${TAG}${NC}"
else
    TAG="$TAG_INPUT"
    echo -e "${GREEN}使用自定义标签: ${TAG}${NC}"
fi

# 完整的镜像名称
FULL_IMAGE_NAME="${IMAGE_NAME}:${TAG}"

echo ""
echo -e "${GREEN}开始构建镜像: ${FULL_IMAGE_NAME}${NC}"
echo ""

# 执行Docker构建
docker build -t "${FULL_IMAGE_NAME}" \
    --build-arg FRONTEND_PORT=9980 \
    --build-arg BACKEND_PORT=9900 \
    -f Dockerfile .

# 检查构建结果
if [ $? -eq 0 ]; then
    echo ""
    echo -e "${GREEN}======================================${NC}"
    echo -e "${GREEN}✅ 镜像构建成功！${NC}"
    echo -e "${GREEN}镜像名称: ${FULL_IMAGE_NAME}${NC}"
    echo -e "${GREEN}======================================${NC}"
    echo ""
    echo "运行命令:"
    echo -e "${YELLOW}docker run -d -p 9980:9980 -p 9900:9900 ${FULL_IMAGE_NAME}${NC}"
    echo ""
else
    echo ""
    echo -e "${RED}======================================${NC}"
    echo -e "${RED}❌ 镜像构建失败！${NC}"
    echo -e "${RED}请检查上面的错误信息${NC}"
    echo -e "${RED}======================================${NC}"
    exit 1
fi

# 显示镜像信息
echo ""
echo -e "${GREEN}镜像信息:${NC}"
docker images | grep "${IMAGE_NAME}"

echo ""
echo -e "${GREEN}构建完成！${NC}"